<?php
	include dirname(__FILE__).'/../bootstrap/unit.php';

	new sfDatabaseManager($configuration);
//	Doctrine_Core::loadData(sfConfig::get('sf_data_dir').'/fixtures');

	$t = new lime_test(65, new lime_output_color());

    // some models using Expando
    $models=array("Entry", "Setting");

    foreach ($models as $model){

        $t->diag("{$model} model classes");


        $modelTable=Doctrine::getTable($model);
        $modelInstance=$modelTable->createQuery()->addOrderBy("Id")->limit(1)->execute()->getFirst();
        if (empty($modelInstance)){
            $modelInstance=new $model();
            $modelInstance->save();
        }

        // check Doctrine model methods
        $t->diag("{$model} model classes - Doctrine standard methods");
        $modelId=$modelInstance->getId();
        $t->cmp_ok(intval($modelId), "==", $modelId, "getId() returns integer ({$modelId})");

        $t->diag("{$model} model classes - Doctrine Expando set/get methods");
        $modelInstance["test.set"]="test";
        $t->is($modelInstance["test.set"], "test", "Expando field returns previously set value");

        $modelInstance["TesT.Setv123"]="TesT.Set.123";
        $t->is($modelInstance["TesT.Setv123"], "TesT.Set.123", "Expando field returns previously set value with random case name");

        $modelInstance["test.setv123"]="TesT.Set.123";
        $t->is($modelInstance["test.setv123"], "TesT.Set.123", "Expando field returns previously set value with previously used name in all-lower case");

        $t->diag("{$model} model classes - Doctrine Expando methods - fromArray/toArray");
        $modelInstance->fromArray(
            array(
                "test.arraykeyv1" => "test.array.val.1",
                "test.ArrayKeyv2" => "Test.Array.Val.2",
                "test.ArrayKeyv3" => "test.array.val.3",
                "test.arraykeyv3" => "test.array.val.4",//not an error, value must differ from test.Array.Key.3 to check key case lowering
            )
        );
        $t->is($modelInstance["test.arraykeyv1"], "test.array.val.1", "Expando field returns previously fromArray set value");
        $t->is($modelInstance["test.ArrayKeyv2"], "Test.Array.Val.2", "Expando field returns previously fromArray set value with random case name");
        $t->is($modelInstance["test.ArrayKeyv3"], "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");
        $t->is($modelInstance["test.arraykeyv3"], "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");


        $valuesArray=$modelInstance->toArray();
        $t->is($valuesArray["test.arraykeyv1"], "test.array.val.1", "Expando field returns previously fromArray set value");
        $t->ok(!array_key_exists("test.ArrayKeyv2", $valuesArray), "Expando field doesnt return previously fromArray set value with random case name");
        $t->ok($valuesArray["test.arraykeyv2"], "Test.Array.Val.2", "Expando field returns previously fromArray set value with random case name as all lower key");
        $t->ok(!array_key_exists("test.ArrayKeyv3", $valuesArray), "Expando field doesnt return previously fromArray set value with random case name");
        $t->is($valuesArray["test.arraykeyv3"], "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");


        $t->diag("Saving {$model} instance to database");
        $modelInstance->save();

        $t->diag("Loading {$model} instance from database");
        $modelInstance=$modelTable->find($modelId);

        $t->is($modelInstance["test.set"], "test", "Expando field returns previously saved value");
        $t->is($modelInstance["TesT.Setv123"], "TesT.Set.123", "Expando field returns previously saved value with random case name");
        $t->is($modelInstance["test.setv123"], "TesT.Set.123", "Expando field returns previously saved value with previously used name in all-lower case");
        $t->is($modelInstance["test.arraykeyv1"], "test.array.val.1", "Expando field returns previously fromArray saved value");
        $t->is($modelInstance["test.ArrayKeyv2"], "Test.Array.Val.2", "Expando field returns previously fromArray saved value with random case name");
        $t->is($modelInstance["test.ArrayKeyv3"], "test.array.val.4", "Expando field returns previously fromArray saved value with random case previously used name");
        $t->is($modelInstance["test.arraykeyv3"], "test.array.val.4", "Expando field returns previously fromArray saved value with random case previously used name");

        $t->diag("{$model} model classes - Doctrine Expando methods - fromArray/toArray");
        $modelInstance->fromArray(
            array(
                "test.arraykeyv1" => "test.array.val.1",
                "test.ArrayKeyv2" => "Test.Array.Val.2",
                "test.ArrayKeyv3" => "test.array.val.3",
                "test.arraykeyv3" => "test.array.val.4",//not an error, value must differ from test.Array.Key.3 to check key case lowering
            )
        );
        $t->is($modelInstance["test.arraykeyv1"], "test.array.val.1", "Expando field returns previously fromArray set value");
        $t->is($modelInstance["test.ArrayKeyv2"], "Test.Array.Val.2", "Expando field returns previously fromArray set value with random case name");
        $t->is($modelInstance["test.ArrayKeyv3"], "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");
        $t->is($modelInstance["test.arraykeyv3"], "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");


        $valuesArray=$modelInstance->toArray();
        $t->is($valuesArray["test.arraykeyv1"], "test.array.val.1", "Expando field returns previously fromArray set value");
        $t->ok(!array_key_exists("test.ArrayKeyv2", $valuesArray), "Expando field doesnt return previously fromArray set value with random case name");
        $t->ok($valuesArray["test.arraykeyv2"], "Test.Array.Val.2", "Expando field returns previously fromArray set value with random case name as all lower key");
        $t->ok(!array_key_exists("test.ArrayKeyv3", $valuesArray), "Expando field doesnt return previously fromArray set value with random case name");
        $t->is($valuesArray["test.arraykeyv3"], "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");


        $modelInstance->mapValue("test-mapped-value", true);

        // weird method to get model instance fields
        $fields=array();
        foreach ($modelInstance as $key=>$value){
           $fields[]=$key;
        }

        $t->ok(!array_key_exists("test-mapped-value", $fields), "Expando doesnt return mapped value as field");
//
        $expandoFieldValues=$modelInstance->getExpandoFields();
        $expandoFields=array();
        if (sizeof($expandoFieldValues)>0){
            $expandoFields=array_keys($expandoFieldValues);
        }
//
////        echo "fields:";
////        var_dump($fields);
////        echo "Expando fields:";
////        var_dump($expandoFields);
////
////        echo "toArray fields:";
////        var_dump(array_keys($modelInstance->toArray()));
////
////        echo "toArray(false) fields:";
////        var_dump(array_keys($modelInstance->toArray(false)));
////
////        echo "data fields:";
////        var_dump(array_keys($modelInstance->getData()));
////
////        echo "getAccessors() fields:";
////        var_dump(array_keys($modelInstance->getAccessors()));
//
//
        $modelFieldsInExpando=array_intersect($fields, $expandoFields);

        $t->ok(sizeof($modelFieldsInExpando)==0, "Expando doesnt add model field values to expando field");
        if (sizeof($modelFieldsInExpando)>0){
            echo "fields:";
            var_dump($fields);
            echo "Expando fields:";
            var_dump($expandoFields);
            echo "intersection:";
            var_dump($modelFieldsInExpando);
        }

    
        if ($model=="Entry"){
            $tags=$modelInstance->getSavedTagsQuery();
            
            $t->ok($tags instanceOf Doctrine_Query, "Expando doesnt corrupt behaviour GET calls");

            $setTags="testTag1,testTag2";
            $modelInstance->setTags($setTags);
            $modelInstance->save();
            
            $tags=$modelInstance->getSavedTags();

var_dump($tags);
            $t->ok($setTags==implode(",", $tags), "Expando doesnt corrupt behaviour SET calls");

            $expandoFieldValues=$modelInstance->getExpandoFields();
            $expandoFieldsNew=array();
            if (sizeof($expandoFieldValues)>0){
                $expandoFieldsNew=array_keys($expandoFieldValues);
            }

            $t->ok($expandoFields==$expandoFieldsNew, "Expando doesnt save behaviour calls as Expando field values");
        }


    }
